import Vue from 'vue';
import VueRouter from "vue-router";

Vue.use(VueRouter);

import Index from '@/views/Index';
import Login from "@/views/Login";

import System from '@/components/system/route';

const routes = [
    {
        path: '/',
        component: Index,
        children: [
            ...System
        ]

    },
    {
        path: '/index',
        name: 'index',
        redirect: '/'
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    }
];

let router = new VueRouter({
    routes
});

// 检查登录状态
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        window.axios.get('/login', {params: {scope: 'status'}})
            .then(res => {
                if (res.data.code === '0x00009')
                    next('/');
                else
                    next();
            })
    } else {
        window.axios.get('/login', {params: {scope: 'status'}})
            .then(res => {
                if (res.data.code === '0x00009')
                    next();
                else
                    next('/login');
            })
    }
});


export default router